/**
 * @Author fxg1997
 * @version 1.0
*/
import {
  post
} from '@/api/table.js';
import breaDcrumb from '@/components/breadcrumb';
import {
  mapGetters
} from 'vuex';
import {
  codeReg
} from "@/common/RegExp.js";
import { _debounce } from '@/utils/utils'
const mixin = {
  components: {
    breaDcrumb
  },
  data() {
    return {
      /*判断收起展开*/
      showCardNav: true,
      /*列表插叙数据*/
      tableParam: {
        page: 1,
        pageSize: 10,
      },
      /*列表数据*/
      tableData: [],
      /*列表总数*/
      total: 0,
      /*删除使用的id集合*/
      ids: [],
      /*保持排序号的id集合*/
      sortIds: [],
      /*状态*/
      enable: [{
        name: '禁用',
        id: 0
      }, {
        name: '启用',
        id: 1
      }],
      /*深度拷贝fromDate*/
      newfromDate: {},
      /*添加弹窗*/
      addDialog: false,
      /*添加弹窗标题*/
      addDialogTitle: '',
      /*页面级表单加载*/
      tableLoading: false,
      /*页面级树加载*/
      treeLoading: false,
      submitLoading: false,
      /**按钮权限列表*/
      ButtonList: []
    }
  },
  computed: {
    ...mapGetters({
      screenWidth: 'getscreenWidth',
      screenHeight: 'getscreenHeight',
      Collapse: 'getsCollapse'
    }),
    style() {
      let num = 0;
      if (this.pagesType == 0) {
        if (this.Collapse) {
          num = this.screenWidth - 124;
        } else {
          num = this.screenWidth - 220;
        }
      } else {

        if (this.Collapse) {
          num = this.screenWidth - 325;
        } else {
          num = this.screenWidth - 421;
        }
      }

      return 'width:' + num + 'px;';
    }
  },
  mounted() {
    this.ButtonList = this.$ButtonList()
  },
  methods: {
    /*编码格式化，禁止输入中文*/
    changecode(key) {
      this.fromDate[key] = codeReg(this.fromDate[key]);
    },
    /*数组对象深度复制*/
    copy(list) {
      let str = JSON.stringify(list)
      return JSON.parse(str)
    },
    console(data) {
      console.log(JSON.parse(JSON.stringify(data)))
    },
    /*获取列表*/
    async gettableData() {
      this.tableLoading = true
      await post(this.queryPage, this.tableParam).then(res => {
        this.total = res.data.totalRows;
        this.tableData = res.data.data;
        this.tableLoading = false
      });
    },
    /*下载*/
    load() {
      this.$Download(this.DownloadURL, this.tableParam);
    },
    /*关闭添加弹窗并重置数据*/
    resetFromDate() {
      delete this.fromDate.id
      this.addDialog = false
    },
    /*添加*/
    addFromDate(text) {
      this.addDialog = true
      this.$data.fromDate = this.$options.data().fromDate
      this.addDialogTitle = text
      setTimeout(() => {
        this.$refs.reffromDate.clearValidate();
      }, 10)
    },
    /*修改数据打开时*/
    updateFromDate(data, text) {
      this.addDialog = true
      setTimeout(() => {
        this.$refs.reffromDate.clearValidate();
      }, 10)
      this.fromDate = Object.assign({}, this.fromDate, data);
      this.addDialogTitle = text
    },
    /*保存或更新数据*/
    submitFromDate: _debounce(function (data) {
      let obj = {}
      if (data) {
        obj = data
      } else {
        obj = this.fromDate
      }
      this.$refs.reffromDate.validate((valid) => {
        if (valid) {
          this.submitLoading = true;
          post(this.saveOrUpdate, obj).then((res) => {
            this.loading = false;
            this.submitLoading = false;
            if (res.code === 1001) {
              this.resetFromDate()
              this.$message({
                message: '保存成功',
                type: "success",
              });
              this.gettableData()
            }
          });
        } else {
          return false;
        }
      });
    }, 200),
    // submitFromDate(data) {
    //   let obj = {}
    //   if (data) {
    //     obj = data
    //   } else {
    //     obj = this.fromDate
    //   }
    //   this.$refs.reffromDate.validate((valid) => {
    //     if (valid) {
    //       this.submitLoading = true;
    //       post(this.saveOrUpdate, obj).then((res) => {
    //         this.loading = false;
    //         this.submitLoading = false;
    //         if (res.code === 1001) {
    //           this.resetFromDate()
    //           this.$message({
    //             message: '保存成功',
    //             type: "success",
    //           });
    //           this.gettableData()
    //         }
    //       });
    //     } else {
    //       return false;
    //     }
    //   });
    // },
    /*更新排序号*/
    SortNum() {
      post(this.updateSortNum, this.ids).then((res) => {
        if (res.code == 1001) {
          this.gettableData()
          this.$message({
            message: '更新成功',
            type: 'success'
          });
        }

      });
    },
    /*禁用，启用*/
    Update(status) {
      if (this.ids.length == 0) {
        this.$message({
          message: '请选择要操作的数据项',
          type: 'warning'
        });
        return;
      }
      post(this.batchUpdateOrDelete, {
        status: status,
        ids: this.ids
      }).then((res) => {
        if (res.code == 1001) {
          this.gettableData()
          this.$message({
            message: '更新成功',
            type: 'success'
          });
        }
      });
    },
    /**删除*/
    Delete() {
      if (this.ids.length == 0) {
        this.$message({
          message: '请选择要操作的数据项',
          type: 'warning'
        });
        return;
      }
      this.$confirm('此操作将永久删除该信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'bottom_all_two',
        confirmButtonClass: 'bottom_all',
        type: 'warning'
      })
        .then(() => {
          post(this.batchUpdateOrDelete, this.ids).then((res) => {
            if (res.code == 1001) {
              let page = Math.ceil((this.total - this.ids.length) / this.tableParam.pageSize)
              if (this.tableParam.page > Math.ceil(page / this.tableParam.pageSize)) {
                this.tableParam.page = page
              }
              this.gettableData()
              this.$message({
                message: '删除成功',
                type: 'success'
              });
            }
          });
        })
        .catch(() => { });
    },
    /**删除，禁用，启用*/
    UpdateOrDelete(type, status) {
      if (this.ids.length == 0) {
        this.$message({
          message: '请选择要操作的数据项',
          type: 'warning'
        });
        return;
      }
      if (type === 'update_status') {
        this.$confirm('此操作将永久删除该信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          cancelButtonClass: 'bottom_all_two',
          confirmButtonClass: 'bottom_all',
          type: 'warning'
        })
          .then(() => {
            post(this.batchUpdateOrDelete, {
              ids: this.ids,
              status: status,
              type: type
            }).then((res) => {
              if (res.code == 1001) {
                let page = Math.ceil((this.total - this.ids.length) / this.tableParam.pageSize)
                if (this.tableParam.page > Math.ceil(page / this.tableParam.pageSize)) {
                  this.tableParam.page = page
                }
                this.gettableData()
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
              }
            });
          })
          .catch(() => { });
      } else {
        post(this.batchUpdateOrDelete, {
          ids: this.ids,
          status: status,
          type: type
        }).then((res) => {
          if (res.code == 1001) {
            this.gettableData()
            this.$message({
              message: '更新成功',
              type: 'success'
            });
          }
        });
      }
    },
    /*列表多选数据*/
    handleSelectionChange(val) {
      this.ids = [];
      this.sortIds = [];
      val.forEach(item => {
        this.ids.push(item.id);
        this.sortIds.push({
          id: item.id,
          sortNum: item.sortNum
        });
      });
    }
  }
}
export default mixin